package com.wjc.server.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wjc.server.dto.CategoryDto;
import com.wjc.server.dto.CourseCategoryDto;
import com.wjc.server.dto.CourseDto;
import com.wjc.server.dto.PageDto;
import com.wjc.server.entity.CourseCategory;
import com.wjc.server.entity.CourseCategoryExample;
import com.wjc.server.mapper.CourseCategoryMapper;
import com.wjc.server.mapper.MyCourseCategoryMapper;
import com.wjc.server.util.CopyUtil;
import com.wjc.server.util.SnowFlake;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class CourseCategoryService {
    @Resource
    private CourseCategoryMapper courseCategoryMapper;

    @Resource
    private SnowFlake snowFlake;

    @Resource
    private MyCourseCategoryMapper myCourseCategoryMapper;

    //查询
    public void list(PageDto pageDto){
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());
        CourseCategoryExample courseCategoryExample = new CourseCategoryExample();
        List<CourseCategory> courseCategoryList= courseCategoryMapper.selectByExample(courseCategoryExample);
        PageInfo<CourseCategory> pageInfo=new PageInfo<>(courseCategoryList);
        pageDto.setTotal(pageInfo.getTotal());

        List<CourseCategoryDto> courseCategoryDtosList = CopyUtil.copyList(courseCategoryList,CourseCategoryDto.class);
        pageDto.setList(courseCategoryDtosList);

    }

    //保存
    public void save(CourseCategoryDto courseCategoryDto){

        CourseCategory courseCategory=CopyUtil.copy(courseCategoryDto,CourseCategory.class);
        if (ObjectUtils.isEmpty(courseCategory.getId())){
            //新增
            courseCategory.setId(snowFlake.nextId());
            courseCategoryMapper.insert(courseCategory);
        }else {
            courseCategoryMapper.updateByPrimaryKey(courseCategory);
        }

    }

    public void delete(Long id){

        courseCategoryMapper.deleteByPrimaryKey(id);
    }

    //批量添加课程分类信息
    public void saveBatch(CourseDto courseDto){
        CourseCategoryExample example = new CourseCategoryExample();
        example.createCriteria().andCourseIdEqualTo(courseDto.getId());
        courseCategoryMapper.deleteByExample(example);
        //new list 存入CourseCategory对象
        List<CourseCategory> courseCategoryList = new ArrayList<>();
        //获取categorys的list
        List<CategoryDto> categorys = courseDto.getCategorys();
        //获取list大小
        int listSize= categorys.size();
       for (int i=0;i<listSize;i++){
           //从传入的dto获取到一个个categoryDto分类对象
           CategoryDto categoryDto=categorys.get(i);
            //设置courseCategory对象的值
           CourseCategory courseCategory=new CourseCategory();
           courseCategory.setId(snowFlake.nextId());
           courseCategory.setCourseId(courseDto.getId());
           courseCategory.setCategoryId(categoryDto.getId());
           //将courseCategory添加到list集合中
           courseCategoryList.add(courseCategory);
       }
        //自定义mapper，批量插入
        myCourseCategoryMapper.insertCourseCategorys(courseCategoryList);
    }

    //查找课程下的分类信息
    public List<CourseCategoryDto> listByCourse(Long courseId){
        CourseCategoryExample courseCategoryExample=new CourseCategoryExample();
        courseCategoryExample.createCriteria().andCourseIdEqualTo(courseId);
        List<CourseCategory> courseCategoryList = courseCategoryMapper.selectByExample(courseCategoryExample);
        List<CourseCategoryDto> courseCategoryDtos = CopyUtil.copyList(courseCategoryList, CourseCategoryDto.class);
        return  courseCategoryDtos;
    }


}
